import unittest
import threading
import time
from blockingqueue import BlockingQueue


class TestBQueue(unittest.TestCase):
    def setUp(self):
        pass


    def tearDown(self):
        pass

    def testBlockingQueue(self):
        queue = BlockingQueue(10)
        p1 = Producer(queue)
        p2 = Producer(queue)
        c1 = Consumer(queue)
        c2 = Consumer(queue)
        c3 = Consumer(queue)
        c4 = Consumer(queue)

        p1.start()
        p2.start()
        c1.start()
        c2.start()
        c3.start()
        c4.start()
        
        p1.join()
        p2.join()
        c1.join()
        c2.join()
        c3.join()
        c4.join()
    
class Producer(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
        
    def run(self):
        for x in range(100):
            print("make " + str(x))
            self.queue.put(x)
            
            
class Consumer(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
        
    def run(self):
        for x in range(100):
            item = self.queue.take()
            print("use " + str(item))
            time.sleep(3)
            
suite = unittest.TestLoader().loadTestsFromTestCase(TestBQueue)
if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite)